import { _decorator, Component, instantiate, Label, Node, Prefab, RichText, Tween, tween, UIStaticBatchComponent, Vec3 } from 'cc';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import PrefsUtil from '../AshFramework/Utils/PrefsUtil';
import { Constant } from '../AshFramework/Datas/Constant';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import Banner from '../Banner';
import { ItemData } from '../Data';
import { GameManager } from '../GameManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { Item } from './Item';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { DataManager } from '../AshFramework/Managers/DataManager';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';

const { ccclass, property } = _decorator;

@ccclass('ItemDetailPanel')
export class ItemDetailPanel extends Component {
    Panel: Node;
    Item: Node;
    ItemQuality: Node;
    EquipButton: Node;
    UpgrateTip: Node;
    UpgrateButton: Node;
    FreeUpgrateButton: Node;

    ItemNameLabel: Label;
    ItemLvLabel: Label;
    DamageValueLabel: Label;
    PushForceValueLabel: Label;
    DamageRadiusValueLabel: Label;
    UpgrateMoneyLabel: RichText;
    UpgratePiecesLabel: RichText;

    item: Node;
    data: ItemData;

    protected onLoad(): void {
        this.Panel = NodeUtil.GetNode("Panel", this.node);
        this.Item = NodeUtil.GetNode("Item", this.node);
        this.ItemQuality = NodeUtil.GetNode("ItemQuality", this.node);
        this.UpgrateTip = NodeUtil.GetNode("UpgrateTip", this.node);
        this.EquipButton = NodeUtil.GetNode("EquipButton", this.node);
        this.UpgrateButton = NodeUtil.GetNode("UpgrateButton", this.node);
        this.FreeUpgrateButton = NodeUtil.GetNode("FreeUpgrateButton", this.node);
        this.ItemNameLabel = NodeUtil.GetComponent("ItemNameLabel", this.node, Label);
        this.ItemLvLabel = NodeUtil.GetComponent("ItemLvLabel", this.node, Label);
        this.DamageValueLabel = NodeUtil.GetComponent("DamageValueLabel", this.node, Label);
        this.PushForceValueLabel = NodeUtil.GetComponent("PushForceValueLabel", this.node, Label);
        this.DamageRadiusValueLabel = NodeUtil.GetComponent("DamageRadiusValueLabel", this.node, Label);
        this.UpgrateMoneyLabel = NodeUtil.GetComponent("UpgrateMoneyLabel", this.node, RichText);
        this.UpgratePiecesLabel = NodeUtil.GetComponent("UpgratePiecesLabel", this.node, RichText);
    }

    Show(data: ItemData) {
        if (Banner.NeedShowMoreAd) {
            Banner.Instance.ShowBannerAd();
            Banner.Instance.ShowCustomAd();
        }

        this.data = data;

        if (this.item) PoolManager.PutNode(this.item);
        PoolManager.GetNode("Prefabs/UI/Item", this.Item).then((item: Node) => {
            this.item = item;
            item.getComponent(Item).InitSimple(data);
        });

        this.Refresh();

        this.Panel.setScale(0, 0, 0);
        Tween.stopAllByTarget(this.Panel);
        tween(this.Panel).to(0.3, { scale: Vec3.ONE }, { easing: `backOut` }).call(() => { }).start();
    }

    Refresh() {
        this.ItemNameLabel.string = `${this.data.Name}`;
        this.ItemLvLabel.string = `Lv.${this.data.Lv}`;
        this.DamageValueLabel.string = `${this.data.Damage}`;
        this.PushForceValueLabel.string = `${this.data.PushForce}`;
        this.DamageRadiusValueLabel.string = `${this.data.DamageRaduis}`;

        this.ItemQuality.children.forEach(e => e.active = e.name == `${this.data.Quality}`);

        let enoughMoney = GameManager.Instance.Money >= this.data.UpgratePrice;
        this.UpgrateMoneyLabel.string = `<outline color=black width=2><color=${enoughMoney ? "green" : "red"}>${GameManager.Instance.Money}</color>/<color=white>${this.data.UpgratePrice}</color></outline>`;
        let enoughPieces = this.data.Piece >= this.data.UpgratePiece;
        this.UpgratePiecesLabel.string = `<outline color=black width=2><color=${enoughPieces ? "green" : "red"}>${this.data.Piece}</color>/<color=white>${this.data.UpgratePiece}</color></outline>`;

        this.EquipButton.active = this.data.IsUnlock && !GameManager.Instance.IsEquipped(this.data.Index);
        this.UpgrateButton.active = enoughMoney && enoughPieces && !this.data.IsMaxLv;
        this.FreeUpgrateButton.active = !(enoughMoney && enoughPieces) && !this.data.IsMaxLv;
        this.item?.getComponent(Item).Refresh();
    }

    OnButtonClick(event, data) {
        AudioManager.PlaySound(Audios.ButtonClick);

        switch (event.target.name) {
            case 'EquipButton':
                UIManager.GetInstance().HidePanel(Panel.ItemDetailPanel);

                if (DataManager.equippedItemDatas.length < ItemData.MaxEquipItem) {
                    DataManager.AddEquippedItem(this.data);
                    EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);
                } else {
                    UIManager.GetInstance().ShowPanel(Panel.DeckPanel, [this.data]);
                }
                break;

            case 'UpgrateButton':
                GameManager.Instance.Money -= this.data.UpgratePrice;
                this.data.Piece -= this.data.UpgratePiece;
                this.data.Lv++;
                this.Refresh();
                EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);
                break;

            case 'FreeUpgrateButton':
                Banner.Instance.ShowVideoAd(() => {
                    this.data.Lv++;
                    this.Refresh();
                    EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);
                });
                break;

            case 'CloseButton':
                UIManager.GetInstance().HidePanel(Panel.ItemDetailPanel);
                break;

            case 'Mask':
                UIManager.GetInstance().HidePanel(Panel.ItemDetailPanel);
                break;

        }
    }

}